/*
 *  Confidential and Proprietary                                                                
 Copyright 2008 By                                                                                     
 SGAI & Hewlett-Packard Development Company, L.P. 	              
 All Rights Reserved                                                                                  

 Project Name : SGAI  MES                                                                                                                                       
 Class Name   : StringConverter.java    
 Package      : com.hp.message.vo.converter      
 */
package com.erdos.message.vo.converter;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

/**
 *String converter.
 @version     $Id: StringConverter.java 9110 2012-03-08 02:08:48Z hubin $                                                          
 @author sunf
 @since  2008-11-11 
 */
public class StringConverter implements Converter {

	@SuppressWarnings("unchecked")
	public boolean canConvert(Class type) {
		return String.class.equals(type);
	}

	public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
		if(source==null ) {
			writer.setValue("");
		} else {
			writer.setValue((String)source);
		}		
	}

	public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
		if(null !=reader.getValue()) {
			return reader.getValue().trim();
		} else {
			return null;
		}		
	}
}
